<style lang="less">
	.hmOrderItem{
		.ivu-modal {
			width: 660px !important;
		}
	}
</style>
<template>
	<Modal v-model="show" :title="editTitle" class="hmOrderItem"  :mask-closable="false">
		<Form ref="hmOrderItemForm" :model="hmOrderItemForm" :rules="hmOrderItemFormRule" :label-width="100">
		</Form>
		<div slot="footer">
			<Button type="text" @click="show=false">取消</Button>
			<Button type="primary" :loading="loading" @click="handSubmit" v-if="!disabled">提交</Button>
		</div>
	</Modal>
</template>
<script>
	import {addHmOrderItem,updateHmOrderItem,getHmOrderItem} from '@/api/hmzd/hmOrderItem'
	export default {
		name: "updateHmOrderItem",
		props: {
			value: {
				type: Boolean,
				default: false
			},
			HmOrderItemId: {
				type: String
			},
			modalTitle: {
				type: String
			}
		},
		data() {
			return {
				show: this.value,
				editTitle: this.modalTitle,
				loading: true,
				disabled:false,
				hmOrderItemForm: {
				},
				hmOrderItemFormRule: this.getHmOrderItemFormRule()
			}
		},
		methods: {
			handSubmit() {
				this.$refs['hmOrderItemForm'].validate((valid) => {
					this.loading = true;
					if (valid) {
						if(this.HmOrderItemId!=null&&this.HmOrderItemId.trim().length>0){
							this.hmOrderItemForm.id=this.HmOrderItemId;
							updateHmOrderItem(this.hmOrderItemForm).then(res => {
								if (res.success) {
									this.closeModal(false);
									this.$emit('handSearch');
									this.$Message.success('保存成功');
								}else{
									this.$Message.error(res.msg);
								}
							}).finally(() => {
								this.loading = false;
							});
						}else{
							addHmOrderItem(this.hmOrderItemForm).then(res => {
								if (res.success) {
									this.closeModal(false);
									this.$emit('handSearch');
									this.$Message.success('保存成功');
								}else{
									this.$Message.error(res.msg);
								}
							}).finally(() => {
								this.loading = false;
							});
						}
					} else {
							this.loading = false;
						this.$Message.error('表单验证不通过！');
					}
				});
			},
			closeModal(val) {
				this.$emit('input', val);
			},
			initForm(){
				this.hmOrderItemForm= {
				};
			},
			getHmOrderItemFormRule() {
				return {
				}
			}
		},
		watch: {
			value(val) {
				this.show = val;
			},
			show(val) {
				this.initForm();
				this.loading=false;
				this.editTitle = this.modalTitle;
				if(this.editTitle == "查看"){
					this.disabled = true;
				}
				else{
					this.disabled = false;
				}
				if(val) {
					this.$refs['hmOrderItemForm'].resetFields();
					this.$refs['hmOrderItemForm'].id = null;
					if(this.HmOrderItemId!=null&&this.HmOrderItemId.trim().length>0){
						getHmOrderItem({id: this.HmOrderItemId}).then(res => {
								if (res.success) {
									 this.hmOrderItemForm = res.data;
							} else {
								this.$Message.error(res.msg);
							}
						});
					}
				} else {
					this.closeModal(val);
				}
			}
		}
	}
</script>
